package com.plan.car.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.plan.car.entity.Dept;
import com.plan.car.entity.Role;
import com.plan.car.entity.User;
import com.plan.car.entity.dto.DeptDTO;
import com.plan.car.entity.vo.DeptVO;
import com.plan.car.service.DeptService;
import com.plan.car.service.RoleService;
import com.plan.car.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门管理")
public class DeptController {
    @Resource
    private DeptService deptService;
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    @GetMapping("/list")
    @ApiOperation("查询部门列表接口")
    public SaResult getDeptList(){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                List<Dept> deptList = deptService.list();
                List<DeptVO> deptVOList = new ArrayList<>();
                for(Dept dept:deptList){
                    DeptVO deptVO = new DeptVO();
                    deptVO.setDeptName(dept.getDeptName());
                    deptVO.setDeptId(dept.getDeptId());
                    deptVOList.add(deptVO);
                }
                return SaResult.data(deptVOList);
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("新增部门接口")
    @PostMapping("/add")
    public SaResult addDept(@RequestBody DeptDTO deptDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                Dept dept = new Dept();
                dept.setDeptName(deptDTO.getDeptName());
                dept.setCreateBy(userId);
                dept.setCreateTime(new Date());
                deptService.save(dept);
                return SaResult.ok("新增部门成功");
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("删除部门接口")
    @PostMapping("/delete")
    public SaResult deleteDept(@RequestBody DeptDTO deptDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                deptService.removeById(deptDTO.getDeptId());
                return SaResult.ok("删除部门成功");
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("修改部门信息接口")
    @PostMapping("/edit")
    public SaResult editDept(@RequestBody DeptDTO deptDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                Dept dept = new Dept();
                dept.setDeptId(deptDTO.getDeptId());
                dept.setDeptName(deptDTO.getDeptName());
                dept.setUpdateBy(userId);
                dept.setUpdateTime(new Date());
                deptService.updateById(dept);
                return SaResult.ok("修改部门信息成功");
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("查询部门信息接口")
    @PostMapping("/select")
    public SaResult selectDept(@RequestBody DeptDTO deptDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                try{
                    Dept deptInfo = deptService.getById(deptDTO.getDeptId());
                    DeptVO deptVO = new DeptVO();
                    deptVO.setDeptId(deptInfo.getDeptId());
                    deptVO.setDeptName(deptInfo.getDeptName());
                    return SaResult.data(deptVO);
                }catch (NullPointerException e){
                    return SaResult.error("部门不存在");
                }
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }
}
